package com.lcxw.hxzbapi.scheduleTask;


import com.alibaba.fastjson.JSONObject;
import com.lcxw.hxzbapi.controller.roomcontroller.Qiniu;
import com.lcxw.hxzbapi.controller.roomcontroller.RoomRealization;
import com.lcxw.hxzbapi.entity.pojo.Duanwei;
import com.lcxw.hxzbapi.entity.pojo.RichLevel;
import com.lcxw.hxzbapi.entity.pojo.Robot;
import com.lcxw.hxzbapi.entity.pojo.VipLevel;
import com.lcxw.hxzbapi.service.DuanweiService;
import com.lcxw.hxzbapi.service.RichLevelService;
import com.lcxw.hxzbapi.service.RobotService;
import com.lcxw.hxzbapi.service.VipLevelService;
import com.lcxw.hxzbapi.service.YwpdRoomService;
import com.lcxw.hxzbapi.tencentim.common.RobotsUtils;
import com.lcxw.hxzbapi.tencentim.common.TencentImRealization;
import com.lcxw.hxzbcommon.common.qiniu.PiliException;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import javax.annotation.Resource;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

@Component
@Slf4j
public class RobotScheduled {

    @Resource
    RobotsUtils robotsUtils;

    @Resource
    RobotService robotService;

    @Resource
    TencentImRealization tencentImRealization;

    @Resource
    VipLevelService vipLevelService;

    @Resource
    DuanweiService duanweiService;

    @Resource
    RichLevelService richLevelService;

    @Resource
    YwpdRoomService ywpdRoomService;

    @Resource
    Qiniu qiniu;

    @Resource
    RoomRealization roomRealization;

    private Random random = new Random();

    @SneakyThrows
    @Scheduled(cron = "0 * * * * ?")
    void robotAction() {

        List<Long> roomIds = ywpdRoomService.selectAllLivingRoomId();
        List<String> livingRoomIdList = new ArrayList<>();

        for (Long roomId : roomIds) {
            try {
                qiniu.getLiveStatus(roomId);
                livingRoomIdList.add(roomId + "");
            } catch (PiliException e) {
                livingRoomIdList.remove(roomId + "");
            }
        }

        //生成一个 机器人动作的次数， 次数为当前在线房间的次数
        int times = livingRoomIdList.size();

        for (int i = 0; i < times; i++) {
            //随机获取一个房间 的roomId
            String roomId = livingRoomIdList.get(random.nextInt(livingRoomIdList.size()));
            robotJoinRoom(roomId);
            Thread.sleep(2000);
            robotSendMsg(roomId);
        }


    }

    /**
     * 机器人进入房间的方法
     */
    private void robotJoinRoom(String roomId) {
        Robot robot = getARandomRobot(1);

        roomRealization.redisAddViewers(roomId, robot.getUserId());
        roomRealization.viewersChange(roomId);

        String msgWelcome = robotsUtils.getMsgWelcome(robot.getNickName());
        JSONObject imBody_welcome = robotsUtils
            .generateImCustomMsg(RobotsUtils.MSG_WELCOME, msgWelcome, roomId);
        tencentImRealization.sendGroupMsg(imBody_welcome);

        RichLevel richLevel = getRandomRichLevel();

        String msgWelcomeAnimate = robotsUtils
            .getMsgWelcomeAnimate(robot.getNickName(), robot.getImgUrl(), richLevel.getLevel(),
                getRandomVipPicUrl(),
                getRandomDuanweiPicUrl(), richLevel.getPicUrl());
        JSONObject imBody_welcome_animate = robotsUtils
            .generateImCustomMsg(RobotsUtils.MSG_WELCOME_ANIMATE, msgWelcomeAnimate, roomId);
        tencentImRealization.sendGroupMsg(imBody_welcome_animate);
    }

    /**
     * 机器人发送弹幕消息的方法
     */
    private void robotSendMsg(String roomId) {
        Robot robot = getARandomRobot(1);

        String msgNormal = robotsUtils
            .getMsgNormal(robot.getNickName(), robot.getSignature(), getRandomVipPicUrl(),
                getRandomDuanweiPicUrl(), getRandomRichLevel().getPicUrl());
        JSONObject imBody_normal_msg = robotsUtils
            .generateImCustomMsg(RobotsUtils.MSG_NORMAL, msgNormal, roomId);
        tencentImRealization.sendGroupMsg(imBody_normal_msg);
    }

    /**
     * 随机获取一个机器人
     */
    private Robot getARandomRobot(int num) {
        List<Robot> robots = robotService.getRandomList(num);
        return robots.get(random.nextInt(robots.size()));
    }

    /**
     * 随机获取一个vip等级的图标地址
     */
    private String getRandomVipPicUrl() {
        List<VipLevel> vipLevels = vipLevelService.selectAll().subList(0, 4);
        VipLevel vipLevel = vipLevels.get(random.nextInt(vipLevels.size()));
        return vipLevel.getPicUrl();
    }

    /**
     * 随机获取一个段位等级的图标地址
     */
    private String getRandomDuanweiPicUrl() {
        List<Duanwei> duanweis = duanweiService.selectAll().subList(0, 4);
        Duanwei duanwei = duanweis.get(random.nextInt(duanweis.size()));
        return duanwei.getPicUrl();
    }

    /**
     * 随机获取一个富豪等级的图标地址
     */
    private RichLevel getRandomRichLevel() {
        List<RichLevel> richLevels = richLevelService.selectAll().subList(0, 4);
        return richLevels.get(random.nextInt(richLevels.size()));
    }


}
